/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : course_write.js                                 
*  Description  : 코스공략 글쓰기 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-30  김효순    Created.  
*  2012-05-02  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	// 제목
	function clsTitle()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "제목을 입력해 주십시요."
			, "_MAXLENTH": "한글 30자 이내로 입력해 주십시요."
		}
		this.errMsg = null;
		this._init();
	}
	
	clsTitle.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content input:text:first");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			this.errMsg = null;
			
			if(this.getValue() == ""){
				this.errMsg = "_NOVALUE";
				return false;
			}else if(_util.getByteLen(this.getValue()) > 60){
				this.errMsg = "_MAXLENTH";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 코스정보
	function clsCourseInfo()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._ERRMSG = {
			"NOVALUE_AREA": "지역을 선택해 주십시요."
			, "NOVALUE_GOLF": "골프장을 선택해 주십시요."
			, "NOVALUE_COURSE": "코스를 선택해 주십시요."
			, "NOVALUE_HOLE": "홀을 선택해 주십시요."
			, "NOVALUE_GOLFNAME": "골프장명을 입력해 주십시요."
			, "NOVALUE_COURSENAME": "코스명을 입력해 주십시요."
			, "NOVALUE_HOLENAME": "홀을 입력해 주십시요."
		}
		this.errMsg = null;
		this.$errObj = null;
		
		this._init();
	}
	
	clsCourseInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content dl.golf_write02>dd:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:radio").attr("name", "rdoCourseInfo");
			
			$layer.find("input:text:eq(0)").attr({
				"placeholder": "골프장 입력"
				, "maxlength": 10
			}).val("");
			$layer.find("input:text:eq(1)").attr({
				"placeholder": "코스 입력"
				, "maxlength": 10
			}).val("");
			$layer.find("input:text:eq(2)").attr({
				"placeholder": "홀"
				, "maxlength": 2
			}).val("");
			
			this.listData = {};
			
			this.setAreaList([]);
			this.setGolfList([]);
			this.setCourseList([]);
			this.setHoleList([]);
			
			this.setTypeIdx(0);
			this.setType();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setAreaList: function(thisData){
			this.listData["지역"] = [{
				"지역코드": ""
				, "지역명": "지역선택"
			}].concat(thisData);
			this.displayArea();
		}
		, setGolfList: function(thisData){
			this.listData["골프장"] = [{
				"골프장코드": ""
				, "골프장명": "골프장선택"
			}].concat(thisData);
			this.displayGolf();
		}
		, setCourseList: function(thisData){
			this.listData["코스"] = [{
				"코스코드": ""
				, "코스명": "코스선택"
			}].concat(thisData);
			this.displayCourse();
		}
		, setHoleList: function(thisData){
			this.listData["홀"] = [{
				"홀코드": ""
				, "홀명": "홀선택"
				, "파일명": ""
			}].concat(thisData);
			this.displayHole();
		}
		, displayArea: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			$layer.find("select:eq(0)").empty();
			for(var i=0;i<this.listData["지역"].length;i++){
				$layer.find("select:eq(0)").append("<option />");
			}
			$layer.find("select:eq(0) option").each(function(n){
				$(this).text(thisClass.listData["지역"][n]["지역명"]);
			});
		}
		, displayGolf: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			$layer.find("select:eq(1)").empty();
			for(var i=0;i<this.listData["골프장"].length;i++){
				$layer.find("select:eq(1)").append("<option />");
			}
			$layer.find("select:eq(1) option").each(function(n){
				$(this).text(thisClass.listData["골프장"][n]["골프장명"]);
			});
		}
		, displayCourse: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			$layer.find("select:eq(2)").empty();
			for(var i=0;i<this.listData["코스"].length;i++){
				$layer.find("select:eq(2)").append("<option />");
			}
			$layer.find("select:eq(2) option").each(function(n){
				$(this).text(thisClass.listData["코스"][n]["코스명"]);
			});
		}
		, displayHole: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			$layer.find("select:eq(3)").empty();
			for(var i=0;i<this.listData["홀"].length;i++){
				$layer.find("select:eq(3)").append("<option />");
			}
			$layer.find("select:eq(3) option").each(function(n){
				$(this).text(thisClass.listData["홀"][n]["홀명"]);
			});
		}
		, getError: clsTitle.prototype.getError
		, valid: function(){
			var $layer = this.$getLayer();
			
			this.errMsg = null;
			this.$errObj = null;
			
			switch(this.getTypeIdx()){
				case 0:
					if(!this.getAreaKey()){
						this.errMsg = "NOVALUE_AREA";
						this.$errObj = $layer.find("select:eq(0)");
						return false;
					}else if(!this.getGolfKey()){
						this.errMsg = "NOVALUE_GOLF";
						this.$errObj = $layer.find("select:eq(1)");
						return false;
					}else if(!this.getCourseKey()){
						this.errMsg = "NOVALUE_COURSE";
						this.$errObj = $layer.find("select:eq(2)");
						return false;
					}else if(!this.getHoleKey()){
						this.errMsg = "NOVALUE_HOLE";
						this.$errObj = $layer.find("select:eq(3)");
						return false;
					}
					
					break;
				case 1:
					if(!this.getGolfValue()){
						this.errMsg = "NOVALUE_GOLFNAME";
						this.$errObj = $layer.find("input:text:eq(0)");
						return false;
					}else if(!this.getCourseValue()){
						this.errMsg = "NOVALUE_COURSENAME";
						this.$errObj = $layer.find("input:text:eq(1)");
						return false;
					}else if(!this.getHoleValue()){
						this.errMsg = "NOVALUE_HOLENAME";
						this.$errObj = $layer.find("input:text:eq(2)");
						return false;
					}
				
					break;
			}
			
			return true;
		}
		, getTypeIdx: function(){
			var $layer = this.$getLayer();
			
			return $layer.find("input:radio").index($layer.find("input:radio:checked"));
		}
		, setTypeIdx: function(n){
			var $layer = this.$getLayer();
			$layer.find("input:radio").eq(n).trigger("click");
		}
		, focus: function(){
			if(this.$errObj){
				this.$errObj.trigger("focus");드
			}else{
				this._HTMLDom["$layer"].find("select:first").trigger("focus");
			}
		}
		, setType: function(){
			var $layer = this.$getLayer();
			var n = this.getTypeIdx();
						
			$layer.find("p").hide();
			$layer.find("p").eq(n).show();
		}
		, getAreaKey: function(){
			var $layer = this.$getLayer();
			var n = $layer.find("select:eq(0) option").index($layer.find("select:eq(0) option:selected"));
			
			if(this.getTypeIdx()){
				return "";
			}else{
				return this.listData["지역"][n]["지역코드"];
			}
		}
		, setAreaKey: function(key){
			var $layer = this.$getLayer();
			
			for(var i=0;i<this.listData["지역"].length;i++){
				if(this.listData["지역"][i]["지역코드"]+"" == key+"") break;
			}
			
			if(i < this.listData["지역"].length){
				$layer.find("select:eq(0) option").eq(i).prop("selected", true);
				$layer.find("select:eq(0)").trigger("change");
			}
		}
		, getGolfKey: function(){
			var $layer = this.$getLayer();
			var n = $layer.find("select:eq(1) option").index($layer.find("select:eq(1) option:selected"));
			
			if(this.getTypeIdx()){
				return "";
			}else{
				return this.listData["골프장"][n]["골프장코드"];
			}
		}
		, setGolfKey: function(key){
			var $layer = this.$getLayer();
			
			for(var i=0;i<this.listData["골프장"].length;i++){
				if(this.listData["골프장"][i]["골프장코드"]+"" == key+"") break;
			}
			
			if(i < this.listData["골프장"].length){
				$layer.find("select:eq(1) option").eq(i).prop("selected", true);
				$layer.find("select:eq(1)").trigger("change");
			}
		}
		, getGolfValue: function(){
			var $layer = this.$getLayer();
			
			if(this.getTypeIdx()){
				return $layer.find("input:text:eq(0)").val();
			}else{
				return "";
			}
		}
		, setGolfValue: function(val){
			var $layer = this.$getLayer();
			
			$layer.find("input:text:eq(0)").val(val);
		}
		, getCourseKey: function(){
			var $layer = this.$getLayer();
			var n = $layer.find("select:eq(2) option").index($layer.find("select:eq(2) option:selected"));
			
			if(this.getTypeIdx()){
				return "";
			}else{
				return this.listData["코스"][n]["코스코드"];
			}
		}
		, setCourseKey: function(key){
			var $layer = this.$getLayer();
			
			for(var i=0;i<this.listData["코스"].length;i++){
				if(this.listData["코스"][i]["코스코드"]+"" == key+"") break;
			}
			
			if(i < this.listData["코스"].length){
				$layer.find("select:eq(2) option").eq(i).prop("selected", true);
				$layer.find("select:eq(2)").trigger("change");
			}
		}
		, getCourseValue: function(){
			var $layer = this.$getLayer();
			
			if(this.getTypeIdx()){
				return $layer.find("input:text:eq(1)").val();
			}else{
				return "";
			}
		}
		, setCourseValue: function(val){
			var $layer = this.$getLayer();
			
			$layer.find("input:text:eq(1)").val(val);
		}
		, getHoleKey: function(){
			var $layer = this.$getLayer();
			var n = $layer.find("select:eq(3) option").index($layer.find("select:eq(3) option:selected"));
			
			if(this.getTypeIdx()){
				return "";
			}else{
				return this.listData["홀"][n]["홀코드"];
			}
		}
		, setHoleKey: function(key){
			var $layer = this.$getLayer();
			
			for(var i=0;i<this.listData["홀"].length;i++){
				if(this.listData["홀"][i]["홀코드"]+"" == key+"") break;
			}
			
			if(i < this.listData["홀"].length){
				$layer.find("select:eq(3) option").eq(i).prop("selected", true);
				$layer.find("select:eq(3)").trigger("change");
			}
		}
		, getHoleValue: function(){
			var $layer = this.$getLayer();
			
			if(this.getTypeIdx()){
				return $layer.find("input:text:eq(2)").val();
			}else{
				return "";
			}
		}
		, setHoleValue: function(val){
			var $layer = this.$getLayer();
			
			$layer.find("input:text:eq(2)").val(val);
		}
		, getImage: function(){
			var $layer = this.$getLayer();
			var n = $layer.find("select:eq(3) option").index($layer.find("select:eq(3) option:selected"));
			
			return this.listData["홀"][n]["파일명"];
		}
		, getTypeKey: function(){
			var ret = ["N", "Y"];
			
			return ret[this.getTypeIdx()];
		}
	}
	
	// 내용
	function clsNote()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"NOVALUE": "내용을 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsNote.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content dl.golf_write02 dd.img_g");
			var thisClass = this;
			this._HTMLDom = {
				"$layer": $layer
			}
			$layer.html("<textarea></textarea>");
			
			fnLoadingJS(_ROOT + "/inc/ckeditor_3.6.3/ckeditor.js", function(){
				fnLoadingJS(_ROOT + "/inc/ckeditor_3.6.3/adapters/jquery.js", function(){
					thisClass._CKEditor();
				});
			});
		}
		, _CKEditor: function(){
			var thisClass = this;
			
			this._HTMLDom["$layer"].find("textarea").ckeditor(document.objGlobal.getCKOption());
				
			// 에디터 준비완료
			this.$getEditor().ckeditorGet().on("instanceReady", function(){
				document.objGlobal.setCKEditorUploadFileButton(this, function(ret){
					document.objControl.finishUploadFile(ret);
				});
			});
		}
		, insertHoleImage: function(szImage){
			var $layer = this._HTMLDom["$layer"];
			var val = $layer.find("textarea").val();
			
			if(val) val = "<br><br>" + val;
			
			this.$getEditor().val("<img src='" + szImage + "' />" + val);
		}
		, focus: function(){
			this.$getEditor().ckeditorGet().focus();
		}
		, $getEditor: function(){
			return this._HTMLDom["$layer"].find("textarea");
		}
		, getValue: function(){
			return this.$getEditor().val();
		}
		, setValue: function(val){
			this.$getEditor().val(val);
		}
		, insertImage: function(szImage){
			this.$getEditor().ckeditorGet().insertHtml("<img src='" + szImage + "' />");
		}
		, getError: clsTitle.prototype.getError
		, valid: function(){
			this.errMsg = null;
			if(!this.getValue()){
				this.errMsg = "NOVALUE";
				return false;
			}
			
			return true;
		}
	}
	
	// 업로드파일
	function clsUploadFile()
	{
		this.listData = null;
		
		this._init();
	}
	
	clsUploadFile.prototype = {
		_init: function(){
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
		}
		, push: function(oneData){
			if(!oneData["파일키"]) oneData["파일키"] = "NOT";
			if(!oneData["섬네일"]) oneData["섬네일"] = "NOT";
			
			this.listData.push(oneData);
		}
		, getList: function(){
			return this.listData;
		}
		, sync: function(note){
			for(var i=0;i<this.listData.length;i++){
				var targetURL = "";
				
				targetURL = targetURL = document.objGlobal.filePath2Url(this.listData[i]["파일명"]);
				/*if(this.listData[i]["파일키"] == "NOT"){
					targetURL = document.objGlobal.
					targetURL = this.listData[i]["파일명"].replace(_UPLOADPATH_TMP, _UPLOADURL_TMP);
				}else{
					targetURL = document.objGlobal.filePath2Url(this.listData[i]["파일명"]);
					//targetURL = this.listData[i]["파일명"].replace(_UPLOADPATH, _UPLOADURL);
				}*/
				
				var reg = new RegExp(targetURL, "gi");
				
				if(!reg.test(note)){
					this.listData.splice(i, 1);
					i--;
				}
			}
		}
		, getValue: function(){
			var szRet = "";
			for(var i=0;i<this.listData.length;i++){
				if(szRet) szRet += ",";
				
				szRet += this.listData[i]["파일키"] + "," + this.listData[i]["섬네일"] + "," + this.listData[i]["파일명"] + "," + this.listData[i]["원본파일명"];
			}
			
			return szRet;
		}
	}
	
	function clsComm()
	{
		this.courseList = null;
		this.courseData = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 골프장 리스트
		, getGolfList: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){};
			
			//ajax
			var serviceName = "getNationalGolfclubList";
			var options = {
				"districts": arg["지역코드"]
			}
			_gf.ajax({
				"sid": "KB001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"골프장코드": _util.nvl(obj["golfclub__seq"])
							, "골프장명": _util.nvl(obj["golfclub__name"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 코스 리스트
		, getCourseList: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){};
			
			//ajax
			var serviceName = "getNationalGolfclubCourseList";
			var options = {
				"golfclub_seq": arg["골프장코드"]
			}
			_gf.ajax({
				"sid": "KB001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"코스코드": _util.nvl(obj["course__seq"])
							, "코스명": _util.nvl(obj["course__name"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 홀 리스트
		, getHoleList: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){};
			
			//ajax
			var serviceName = "getNationalGolfclubHoleList";
			var options = {
				"course_seq": arg["코스코드"]
			}
			_gf.ajax({
				"sid": "KB001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"홀코드": _util.nvl(obj["hole__no"])
							, "홀명": _util.nvl(obj["hole__no"])
							, "파일명": _util.nvl(obj["path"]) + _util.nvl(obj["save__file__name"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 코스공략 정보
		, getCourseInfo: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){};
			
			//ajax
			var serviceName = "getDetailKnowhowBoard";
			var options = {
				"board_seq": arg["코스공략코드"]
				, "modpage_yn": "Y"
			}
			document.objGlobal.ajax({
				"sid": "KB001",
				"servicename": "getDetailKnowhowBoard"
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					var reg_date = _util.nvl(obj["reg__date"])+""
					var reg_time = _util.nvl(obj["reg__time"])+""
					
					var golfName = "";
					var courseName = "";
					var hole = "";
					
					switch(_util.nvl(obj["userWriteYn"])){
						case "Y":
							golfName = _util.nvl(obj["golfclub__data"]);
							courseName = _util.nvl(obj["course__data"]);
							hole = _util.nvl(obj["hole__data"]);
							break;
						case "N":
							golfName = _util.nvl(obj["golfclub__name"]);
							courseName = _util.nvl(obj["course__info__name"]);
							hole = _util.nvl(obj["hole__no"]);
							break;
					}
					
					var fileBean = obj["FileBean"];
					var aFileList = [];
					
					if(fileBean){
						for(var i=-0;i<fileBean.length;i++){
							var objFile = fileBean[i]["items"];
							aFileList.push({
								"파일키": objFile["file__seq"]
								, "섬네일": objFile["thumb"]
								, "파일명": objFile["path"] + objFile["save__file__name"]
								, "원본파일명": objFile["orgn__file__name"]
							});
						}
					}
					
					res["결과값"] = {
						"제목": _util.nvl(obj["title"])
						, "직접작성여부": _util.nvl(obj["userWriteYn"])
						, "지역코드": _util.nvl(obj["districts"])
						, "골프장코드": _util.nvl(obj["golfclub__seq"])
						, "코스코드": _util.nvl(obj["course__seq"])
						, "홀코드": _util.nvl(obj["hole__no"])
						, "골프장명": _util.nvl(obj["golfclub__data"])
						, "코스명": _util.nvl(obj["course__data"])
						, "홀명": _util.nvl(obj["hole__data"])
						, "내용": _util.nvl(obj["content"])
						, "파일리스트": aFileList
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 등록
		, courseSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "regKnowhowBoard";
			if(arg["코스공략코드"]) serviceName = "updateKnowhowBoard";
			
			var districts, golfclub_seq, course_seq, hole_no, golfclub_data, course_data, hole_data;
			switch(arg["코스정보타입코드"]){
				case "Y":
					districts = "";
					golfclub_seq = "";
					course_seq = "";
					hole_no = "";
					
					golfclub_data = arg["골프장명"];
					course_data = arg["코스명"];
					hole_data = arg["홀"];
					
					break;
				case "N":
					districts = arg["지역코드"];
					golfclub_seq = arg["골프장코드"];
					course_seq = arg["코스코드"];
					hole_no = arg["홀코드"];
					
					golfclub_data = "";
					course_data = "";
					hole_data = "";
					
					break;
			}
			
			var options = {
				"board_seq": arg["코스공략코드"]
				, "title": _util.ajaxTrans(arg["제목"])
				, "content": _util.ajaxTrans(arg["내용"])
				, "userWriteYn": arg["코스정보타입코드"]
				, "districts": districts
				, "golfclub_seq": golfclub_seq
				, "course_seq": course_seq
				, "hole_no": hole_no
				, "golfclub_data": golfclub_data
				, "course_data": course_data
				, "hole_data": hole_data
				, "manual_course_info": ""
				, "golfclub_id": ""
				, "course_info_id": ""
				, "hole": ""
				, "notice_yn": "N"
				, "file_list": arg["파일리스트"]
			}
			_gf.ajax({
				"sid": "KB001"
				, "servicename": serviceName
			}, options, function(ret){
				document.objControl.finishCourseSubmit({
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				});
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objTitle = null;
		this.objCourseInfo = null;
		this.objNote = null;
		this.objUploadFile = null;
		
		this.ynMy = document.objUtil.getParam("q_my");
		this.courseKey = document.objUtil.getParam("q_seq");
		
		this.courseInfo = null;
		
		this._permitStart = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objTitle = new clsTitle();
			this.objCourseInfo = new clsCourseInfo();
			this.objNote = new clsNote();
			this.objUploadFile = new clsUploadFile();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				"지역": false
			}
			
			_util.executeThread(function(){
				return _gf.getAreaCodeData();
			}, function(){
				thisClass._permitStart["지역"] = true;
			});
			
			
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			this.objCourseInfo.$getLayer().on("click", "input:radio", function(){
				setTimeout(function(){
					thisClass.objCourseInfo.setType();
				}, 500);
			});
			
			this.objCourseInfo.$getLayer().on("change", "select", function(){
				var n = thisClass.objCourseInfo.$getLayer().find("select").index($(this));
				
				switch(n){
					case 0:
						thisClass.getGolfList();
						break;
					case 1:
						thisClass.getCourseList();
						break;
					case 2:
						thisClass.getHoleList();
						break;
				}
			});
			
			// 골프장 선택
//			this.objCourseInfo.$getLayer().on("change", "select:eq(0)", function(){
//				thisClass.objCourseInfo.displayHole();
//				thisClass.objCourseInfo.setDirect();
//			});
			
			// 글쓰기
			$("div#container section#main_content a").has("img[alt=확인]").click(function(){
				// check valid
				if(!thisClass.objTitle.valid()){
					alert(thisClass.objTitle.getError());
					thisClass.objTitle.focus();
					return;
				}else if(!thisClass.objCourseInfo.valid()){
					alert(thisClass.objCourseInfo.getError());
					thisClass.objCourseInfo.focus();
					return;
				}else if(!thisClass.objNote.valid()){
					alert(thisClass.objNote.getError());
					//thisClass.objCourseInfo.focus();
					return;
				}
				
				thisClass.objUploadFile.sync(thisClass.objNote.getValue());
				var szUploadFile = thisClass.objUploadFile.getValue();
				
				_gf.spinStart("sending...");
				thisClass.objComm.courseSubmit({
					"코스공략코드": thisClass.courseKey
					, "제목": thisClass.objTitle.getValue()
					, "내용": thisClass.objNote.getValue()
					, "코스정보타입코드": thisClass.objCourseInfo.getTypeKey()
					, "지역코드": thisClass.objCourseInfo.getAreaKey()
					, "골프장코드": thisClass.objCourseInfo.getGolfKey()
					, "코스코드": thisClass.objCourseInfo.getCourseKey()
					, "홀코드": thisClass.objCourseInfo.getHoleKey()
					, "골프장명": thisClass.objCourseInfo.getGolfValue()
					, "코스명": thisClass.objCourseInfo.getCourseValue()
					, "홀": thisClass.objCourseInfo.getHoleValue()
					, "파일리스트": szUploadFile
				});
			});
			
			// 이미지 삽입
			this.objCourseInfo.$getLayer().on("click", "img[alt='이미지삽입']", function(){
//				var gk = thisClass.objCourseInfo.getGolfKey();
//				
//				if(gk == "_NOT" || !gk) return;
//				
//				// check valid
//				if(!thisClass.objCourseInfo.valid()){
//					alert(thisClass.objCourseInfo.getError());
//					thisClass.objCourseInfo.focus();
//					return;
//				}
				
				thisClass.insertImage();
			});
			
			// 취소
			$("div#container").on("click", "img[alt='취소']", function(){
				thisClass.goBackPage();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.objCourseInfo.setAreaList(_gf.getAreaCodeData());
			
			if(this.courseKey){
				document.objGlobal.spinStart("loading...");
				this.objComm.getCourseInfo({
					"코스공략코드": this.courseKey
					, "콜백": function(ret){
						thisClass.finishCourseInfo(ret);
					}
				});
			}
		}
		// 코스정보 완료
		, finishCourseInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisData = ret["결과값"];
				
			_gf.spinEnd();

			this.objTitle.setValue(thisData["제목"]);
			
			switch(thisData["직접작성여부"]){
				case "Y":
					this.objCourseInfo.setTypeIdx(1);
					this.objCourseInfo.setGolfValue(thisData["골프장명"]);
					this.objCourseInfo.setCourseValue(thisData["코스명"]);
					this.objCourseInfo.setHoleValue(thisData["홀명"]);
					break;
				case "N":
					this.objCourseInfo.setTypeIdx(0);
					this.courseInfo = {
						"골프장코드": thisData["골프장코드"]
						, "코스코드": thisData["코스코드"]
						, "홀코드": thisData["홀코드"]
					}
				
					this.objCourseInfo.setAreaKey(thisData["지역코드"]);
					break;
			}
			
			this.objNote.setValue(thisData["내용"]);
			this.objUploadFile.setList(thisData["파일리스트"]);
		}
		// 골프장 리스트 완료
		, finishGolfList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objCourseInfo.setGolfList(ret["결과값"]);
			
			if(this.courseInfo){
				if(this.courseInfo["골프장코드"]){
					this.objCourseInfo.setGolfKey(this.courseInfo["골프장코드"]);
					
					delete this.courseInfo["골프장코드"];
				}
			}
		}
		// 코스 리스트 완료
		, finishCourseList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objCourseInfo.setCourseList(ret["결과값"]);
			
			if(this.courseInfo){
				if(this.courseInfo["코스코드"]){
					this.objCourseInfo.setCourseKey(this.courseInfo["코스코드"]);
					
					delete this.courseInfo["코스코드"];
				}
			}
		}
		// 홀 리스트 완료
		, finishHoleList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objCourseInfo.setHoleList(ret["결과값"]);
			
			if(this.courseInfo){
				if(this.courseInfo["홀코드"]){
					this.objCourseInfo.setHoleKey(this.courseInfo["홀코드"]);
					
					this.courseInfo = null;
				}
			}
		}
		// 이미지 삽입
		, insertImage: function(){
			var img = this.objCourseInfo.getImage();
			
			if(!img){
				alert("등록된 이미지가 없습니다.");
				return;
			}
			
			this.objNote.insertHoleImage(_gf.filePath2UrlSC(img));
		}
		// 이미지 업로드
		, finishUploadFile: function(ret){
			var fs = ret["파일리스트"][0]["파일크기"]-0;
			
			if(fs >= 1 * 1024 * 1024){
				alert("사진용량은 1장당 1MB까지만 등록 가능합니다. 파일 용량을 확인하여 주세요.");
				return;
			}
			
			this.objNote.insertImage(_gf.filePath2Url(ret["파일리스트"][0]["파일명"]));

			this.objUploadFile.push(ret["파일리스트"][0]);
		}
		// 작성완료
		, finishCourseSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			this.goList();
		}
		// 골프장 리스트 요청
		, getGolfList: function(){
			var thisClass = this;
			
			var ak = this.objCourseInfo.getAreaKey();
			this.objCourseInfo.setHoleList([]);
			this.objCourseInfo.setCourseList([]);
			this.objCourseInfo.setGolfList([]);
			if(!ak) return;
			
			this.objComm.getGolfList({
				"지역코드": ak
				, "콜백": function(ret){
					thisClass.finishGolfList(ret);
				}
			});
		}
		// 코스 리스트 요청
		, getCourseList: function(){
			var thisClass = this;
			
			var gk = this.objCourseInfo.getGolfKey();
			this.objCourseInfo.setHoleList([]);
			this.objCourseInfo.setCourseList([]);
			if(!gk) return;
			
			this.objComm.getCourseList({
				"골프장코드": gk
				, "콜백": function(ret){
					thisClass.finishCourseList(ret);
				}
			});
		}
		// 홀리스트 요청
		, getHoleList: function(){
			var thisClass = this;
			
			var ck = this.objCourseInfo.getCourseKey();
			this.objCourseInfo.setHoleList([]);
			if(!ck) return;
			
			this.objComm.getHoleList({
				"코스코드": ck
				, "콜백": function(ret){
					thisClass.finishHoleList(ret);
				}
			});
		}
		// 리스트이동
		, goList: function(){
			var szLink = _ROOT + "/course/course.html" + _util.paramStringFilter(["q_seq"]);
			document.location.href = szLink;
		}
		// 뒤로가기
		, goBackPage: function(){
			if(this.courseKey){
				var szLink = _ROOT + "/course/course_view.html" + _util.paramStringFilter();
			}else{
				this.goList();
			}
		}
	}
	
	document.objControl = new clsControl();
});
